
'''
https://leetcode.cn/problems/two-sum/description/
两数之和，返回下标
[2,7,11,15] target=9
[0,1]
'''

def l1(nums,target):
    slow = 0
    fast = len(nums) - 1
    n = sorted(nums)
    while fast>slow:
        if n[slow] + n[fast] > target:
            fast -= 1
        elif n[slow] + n[fast] < target:
            slow += 1
        else:
            break
    res = set()
    for i in range(len(nums)):
        if nums[i] == n[slow]:res.add(i)
        if nums[i] == n[fast]:res.add(i)
    return list(res)
    pass

'''
index方法，获取值对应的下标
nums = [10,20,30,40]
nums.index(30) 1
'''
def l1_1(nums,target):
    left = 0
    right = len(nums) - 1
    n = sorted(nums)
    left_index = right_index = 0
    while right>left:
        if n[left] + n[right] > target:
            right -= 1
        elif n[left] + n[right] < target:
            left += 1
        else:
            left_index = nums.index(n[left])
            if n[left] == n[right]:
                right_index = nums[left_index+1:].index(n[left]) + left_index + 1
            else:right_index = nums.index(n[right])
            return [left_index,right_index]
    return []

def l1_2(nums,target):
    s = set()
    for i ,value in enumerate(nums):
        print(i,value)
        c = target -value
        if c in s:
            return [i,nums.index(c)]
        s.add(value)
    return []


if __name__ == '__main__':
    print(l1_2([2,7,11,15],9))
    # print(l1([3,2,4],6))
    # print(l1([3,3],6))
    # print(l1([6,4],6))

    pass